package scau.mis4.chanyulin.atm.base.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by cyl on 2017/4/2
 * 命名转换工具类
 */
public class NameUtils {

    /**
     * 首字母转为小写
     * @param str 字符串
     */
    public static String firstCharToLowerCase(String str){
        if (Character.isLowerCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * 首字母转为大写
     * @param str 字符串
     */
    public static String firstCharToUpperCase(String str){
        if (Character.isUpperCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toUpperCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * 把驼峰风格的字符串转为以空格分割的的字符串
     * @param str 字符串
     * @param returnUpper 返回的单词是否以大写开头
     */
    public static String camelToBlankSpace(String str, boolean returnUpper) {
        Pattern pattern = Pattern.compile("[A-Z][a-z]*");
        Matcher matcher = pattern.matcher(firstCharToUpperCase(str.trim()));
        StringBuffer sb = new StringBuffer();
        if (returnUpper) {
            while (matcher.find()) {
                matcher.appendReplacement(sb, " " + matcher.group(0));
            }
            return sb.toString().trim();
        } else {
            while (matcher.find()) {
                matcher.appendReplacement(sb, " " + firstCharToLowerCase(matcher.group(0)));
            }
            return sb.toString().trim();
        }
    }

}
